<template>
	<view>
		<uni-search-bar :radius="100" @input="input" cancelButton='none'></uni-search-bar>
    <!-- 搜索建议区域 -->
    <view class="sugg-list" v-if="searchResults.length !== 0">
      <view class="sugg-item" v-for="(item,i) in searchResults" :key='i' @click="gotoDetail(item.goods_id)">
        <view class="goods-item">{{item.goods_name}}</view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
    <!-- 搜索历史区域 -->
    <view class="search-history" v-else>
      <!-- 搜索历史标题区域 -->
      <view class="search-history-title">
         <text>搜索历史</text>
         <uni-icons type="trash" size="18" @click="clean"></uni-icons>
      </view>
      <!-- 搜索历史列表 -->
      <view class="search-history">
        <uni-tag :text="item"  v-for="(item,i) in histories" :key="i" @click="gotoGoodsList(item)"></uni-tag>
      </view>
    </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
        // 防抖处理定时器
				timer:null,
        kw:'',
        // 搜索结果列表
        searchResults: [],
        // 搜索历史列表
        historyList: ['a','app','apple']
			};
		},
    // 当窗体一加载时,就取得historyList的值
    onLoad(){
      this.historyList = JSON.parse(uni.getStorageSync("kw") || "[]")
    },
    methods:{
      input(e){
        // 在输入内容时进行防抖处理
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          console.log(e.value)
          this.kw = e.value
          // 开始搜索
          this.getSearchResults()
        },500)
      },
      // 查询搜索列表
      async getSearchResults(){
        if (this.kw.length === 0){
          this.searchResults = []
          return
        }
        const {data:res}  = await uni.$http.get("/api/public/v1/goods/qsearch?query=" + this.kw)
        console.log(res)
        if (res.meta.status !== 200) uni.$showMsg()
        this.searchResults = res.message
        
        // 保存搜索列表
        this.saveHistoryList()
      },
      // 到商品详情页
      gotoDetail(goodsId){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id='+goodsId
        })
      },
      // 保存历史列表
      saveHistoryList(){
         let set = new Set(this.historyList)
         set.delete(this.kw)
         set.add(this.kw)
         this.historyList = Array.from(set)
         // this.historyList.unshift(this.kw)
         // let set = new Set(this.historyList)
         // this.historyList = Array.from(set)
         
         // 保存到本地存储
         uni.setStorageSync("kw",JSON.stringify(this.historyList))
      },
      // 清空历史列表
      clean(){
        this.historyList = []
        uni.setStorageSync("kw","[]")
      },
      // 到商品列表页
      gotoGoodsList(item){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?query=' + item
        })
      }
    },
    // 定义计算属性   
    computed:{
      histories(){
        return [...this.historyList].reverse()
      }
    },
    
    
	}
</script>

<style lang="scss">
.sugg-list{
  padding: 0 5px;
  .sugg-item{
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding:13px 0;
    border-bottom: 1px solid #efefef;
    .goods-item{
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      font-size: 12px;
      margin-right: 2px;
    }
  }
}
.search-history{
  padding: 0 5px;
  .search-history-title{
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 30px;
    border-bottom: 1px solid #efefef;
    text{
      margin-left: 3px;
      font-size: 12px;
      font-weight: bolder;
    }
    .uni-icons{
      margin-right: 3px;
    }
  }
  .search-history{
    display: flex;
     margin-top: 3px;
    .uni-tag{
      margin-right: 3px;
     
    }

  }
}
</style>
